﻿using System;
using System.Windows.Forms;
using Relocation.Data;
using Relocation.Base;
using Relocation.Com;
using Relocation.Com.Tools;
using Relocation.Base.UI;
using System.Linq;

namespace Relocation
{
	public partial class LoginWindow : BaseForm
	{
		public User User
		{
			get;
			private set;
		}

		public LoginWindow()
			: base()
		{
			Initialize();
		}

		public LoginWindow(Session s)
			: base(s)
		{
			Initialize();
		}

		private void Initialize()
		{
			InitializeComponent();
			this.username.Tag = new ControlTag(User.GetPropName(t => t.name), new ValidatorInfo(true));
			this.password.Tag = new ControlTag(User.GetPropName(t => t.password), new ValidatorInfo(true));
		}

        /// <summary>
        /// 重写父类的事件（防止关闭窗体时提示数据未保存）
        /// </summary>		
        protected override void FieldTextChangeEvent(object sender, EventArgs e)
        {
        }

		//登录
		private void submit_Click(object sender, EventArgs e)
		{
			try
			{
				if (!this.ValidatorAllControls())
					return;
				this.User = Session.DataModel.getUserByNameAndPassword(username.Text, password.Text);
				if (User == null)
				{
					this.errLabel.Text = "您输入的用户名/密码错误";
					return;
				}
				this.DialogResult = DialogResult.OK;
				this.Close();
			} catch (Exception ex)
			{
				Log.Error(ex);
				MyMessagebox.Error("连接数据库失败！");
			}
		}

		//重置
		private void reset_Click(object sender, EventArgs e)
		{
			username.Text = "";
			password.Text = "";
			errLabel.Text = "";
		}

		private void username_TextChanged(object sender, EventArgs e)
		{
			this.password.Text = "";
			this.errLabel.Text = "";
		}
	}
}
